package com.weiaibenpao.demo.chislim.ui;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ImageSpan;
import android.view.View;
import android.widget.TextView;

import com.jaeger.library.StatusBarUtil;
import com.weiaibenpao.demo.chislim.R;
import com.weiaibenpao.demo.chislim.adater.MatchListAdapter;
import com.weiaibenpao.demo.chislim.bean.MyGameBean;
import com.weiaibenpao.demo.chislim.model.MyModel;
import com.weiaibenpao.demo.chislim.ui.marathon.CampaignActivity;
import com.weiaibenpao.demo.chislim.ui.marathon.MyGameActivity;
import com.wuxiaolong.pullloadmorerecyclerview.PullLoadMoreRecyclerView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class UnofficialMatchActivity extends AppCompatActivity {
    @BindView(R.id.rv_match_list)
    PullLoadMoreRecyclerView mPullRecycleView;
    @BindView(R.id.tv_launch_btn)
    TextView mLaunchBtn;
    private MatchListAdapter mAdapter;
    private int mPage ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_unofficial_match);
        ButterKnife.bind(this);
        initView();
        mPullRecycleView.refresh();
        //        textView.getBackground().setLevel(1);
    }

    private void initView() {
        StatusBarUtil.setColor(this , getResources().getColor(R.color.blackbg ) , 0);
        //以下功能也可以使用TextView的DrawableLeft属性实现
        SpannableString spannableString = new SpannableString("旗子 发起");
        Drawable drawable = getResources().getDrawable(R.drawable.ic_flag);
        drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
        ImageSpan imageSpan = new ImageSpan(drawable);
        spannableString.setSpan(imageSpan, 0, 2, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        mLaunchBtn.setText(spannableString);
        initRecyclerView();
    }

    public static void openActivity(Context context){
        Intent intent = new Intent(context,UnofficialMatchActivity.class);
        context.startActivity(intent);
    }

    private void initRecyclerView(){
        mAdapter = new MatchListAdapter(this);
        mPullRecycleView.setLinearLayout();
        mPullRecycleView.setAdapter(mAdapter);
        mPullRecycleView.setOnPullLoadMoreListener(new PullLoadMoreRecyclerView.PullLoadMoreListener() {
            @Override
            public void onRefresh() {
//                Log.e("initRecyclerView","onRefresh");
                mPage = 0;
                requestData();
            }

            @Override
            public void onLoadMore() {
                requestData();
            }
        });
        //        mPullRecycleView.getRecyclerView().addOnScrollListener(new RecyclerView.OnScrollListener() {
//            @Override
//            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
//                super.onScrollStateChanged(recyclerView, newState);
//                int pos = ((LinearLayoutManager) mPullRecycleView.getLayoutManager()).findFirstCompletelyVisibleItemPosition();
//                if (pos == 0) {
//                    mPullRecycleView.setPullRefreshEnable(true);
//                } else {
//                    mPullRecycleView.setPullRefreshEnable(false);
//                }
//            }
//
//            @Override
//            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
//
//            }
//        });
    }

    private void requestData(){
//        Log.e("requestData","requestData");
        MyModel.getModel().getService().getUnofficialMatchList(mPage).enqueue(new Callback<MyGameBean>() {
            @Override
            public void onResponse(Call<MyGameBean> call, Response<MyGameBean> response) {
                if(mPullRecycleView != null) mPullRecycleView.setPullLoadMoreCompleted();
                MyGameBean bean = response.body();
                if (bean != null) {
                    if (bean.getCode() == 0) {
                        if(bean.getData() != null && bean.getData().getList()!= null && bean.getData().getList().size()>0){
                            if(mPage == 0){
                                mAdapter.clear();
                            }
                            mAdapter.addData(bean.getData().getList());
                            mPullRecycleView.getRecyclerView().getAdapter().notifyDataSetChanged();
                            mPage++;
                        }
                    }
                }
            }

            @Override
            public void onFailure(Call<MyGameBean> call, Throwable t) {
               if(mPullRecycleView != null) {
                   mPullRecycleView.setPullLoadMoreCompleted();
               }
            }
        });
    }


    @OnClick({R.id.iv_back,R.id.iv_right,R.id.tv_launch_btn})
    public void onClick(View view){
        switch (view.getId()){
            case R.id.iv_back:
                finish();
                break;
            case R.id.iv_right:
                //TODO 跳转我的活动
                MyGameActivity.openActivity(this);
                break;
            case R.id.tv_launch_btn:
                //TODO 跳转发起活动
                CampaignActivity.openActivity(this);
                break;
        }

    }


}
